package chapter13.section8;
//程序13-9利用CachedRowSet对象更新数据

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class CachedRowSetUpdateDemo {
    public static void main(String[] args) throws SQLException {
        String url = "jdbc:mysql://localhost:3306/bank?user=root&password=123456";
        Connection con = DriverManager.getConnection(url);
        RowSetFactory aFactory = RowSetProvider.newFactory();
        CachedRowSet crs = aFactory.createCachedRowSet();
        //首先获得一个没有数据的集合
        crs.setCommand("select * from tb_account where id='0'");
        crs.execute(con);
        crs.moveToInsertRow();
        crs.updateString("id", "0");  //注意关键字的值不要和已有记录重复
        crs.updateString("name", "鲁宁0");
        crs.updateInt("balance", 100);
        crs.insertRow();
        crs.moveToCurrentRow();
        con.setAutoCommit(false);
        crs.acceptChanges();
    }
}